﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;

namespace Human
{
    public partial class frm_ngaynghile : Form
    {
       
        private AccessData mDB = new AccessData();
        Language lan = new Language();
        DataSet ds_letet = new DataSet();
        public frm_ngaynghile()
        {
            InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
        }

        private void frm_ngaynghile_Load(object sender, EventArgs e)
        {
            load_dmletet();
            rad_nghile.Checked=true;
            ena_ctrl(false);
            ena_btn(true);            
        }
    
        private void ena_btn(bool ena)
        {
            btn_them.Enabled = ena;
            btn_sua.Enabled = !ena;
            btn_luu.Enabled = !ena;
            btn_boqua.Enabled = !ena;
            btn_xoa.Enabled = !ena;
            btn_thoat.Enabled = ena;
        }
        private void ena_ctrl(bool ena)
        {
            dtp_ngayle.Enabled = ena;
            txt_lydo.Enabled = ena;
            rad_nghibu.Enabled = ena;
            rad_nghile.Enabled = ena;
        }
        private void clear_ctr()
        {
            txt_lydo.Text = "";
            rad_nghibu.Checked = false;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            ena_ctrl(true);
            clear_ctr();
            ena_btn(false);
            btn_xoa.Enabled = false;
            btn_sua.Enabled = false;
            dtp_ngayle.Focus();
        }

        private void dtp_ngayle_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void txt_lydo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }
        private void rad_nghile_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void rad_nghibu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btn_luu.Focus();
        }     
        private void btnLuu_Click(object sender, EventArgs e)
        {
            int loai = 0;
            string ngay="",lydo = "",mmyy="";
            if (dtp_ngayle.Value.ToString() !="")
            {
                ngay=dtp_ngayle.Value.ToString("dd/MM/yyyy").Trim();
            }
            if(rad_nghile.Checked)
            {
                loai=0;
            }
            if(rad_nghibu.Checked)
            {
                loai=1;
            }
            lydo=txt_lydo.Text.Trim();
            mmyy = ngay.Substring(3, 2) + ngay.Substring(8, 2);
            mDB.Insert_DMNGAYLE(ngay,lydo,loai,mmyy);                   
            ena_ctrl(false);
            ena_btn(true);
            load_dmletet();
        }

        private void btn_boqua_Click(object sender, EventArgs e)
        {
            ena_ctrl(false);
            ena_btn(true);
            btn_them.Focus();
        }
       
        private void btn_sua_Click(object sender, EventArgs e)
        {
            ena_ctrl(true);
            ena_btn(false);
            btn_xoa.Enabled = false;
            txt_lydo.Focus();
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show(lan.Change_language_MessageText("Đồng ý huỷ thông tin này?"),AccessData.Msg, MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                mDB.get_data("delete from " + mDB.user + ".dmngayle where to_date(to_char(ngay,'dd/mm/yyyy'),'dd/mm/yyyy')= to_date('" + dtp_ngayle.Value.ToString("dd/MM/yyyy") + "','dd/mm/yyyy')");
            }            
            load_dmletet();
            clear_ctr();
            ena_ctrl(false);
            ena_btn(true);
            btn_them.Focus();
        }
        
        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void load_dmletet()
        {
            string sql = "";
            sql = " SELECT to_char(ngay,'dd/mm/yyyy') as ngay,to_date(to_char(ngay,'dd/mm/yyyy'),'dd/mm/yyyy') as d_ngay,lydo,loai";
            sql += " ,case when loai=0 then 'Nghỉ lễ' else 'Nghỉ bù lễ' end tenloai";
            sql +=" FROM "+ mDB.user +".dmngayle order by d_ngay";
            ds_letet = mDB.get_data(sql);
            dgrv_letet.DataSource = ds_letet.Tables[0];
        }
        private void dgrv_letet_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataRowView drv = (DataRowView)dgrv_letet.CurrentRow.DataBoundItem;
            string date = drv["ngay"].ToString();
            int dd = int.Parse(date.Substring(0, 2));
            int mm = int.Parse(date.Substring(3, 2));
            int y = int.Parse(date.Substring(6, 4));
            dtp_ngayle.Value = new DateTime(y, mm, dd);
            txt_lydo.Text = drv["lydo"].ToString();
            rad_nghile.Checked = drv["loai"].ToString().Trim().Equals("0");
            rad_nghibu.Checked = drv["loai"].ToString().Trim().Equals("1");
            ena_ctrl(false);
            ena_btn(false);
            btn_luu.Enabled = false;
            btn_thoat.Enabled = true;
            btn_sua.Focus();
        }

        private void rad_nghile_CheckedChanged(object sender, EventArgs e)
        {
            if (rad_nghile.Checked)
            {
                rad_nghibu.Checked = false;
            }
            else
            {
                rad_nghibu.Checked = true;
            }

        }

        private void rad_nghibu_CheckedChanged(object sender, EventArgs e)
        {
            if (rad_nghibu.Checked)
            {
                rad_nghile.Checked = false;
            }
            else
            {
                rad_nghile.Checked = true;
            }
        }        
    }
}